package com.gj.demojuc.chap02;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 使用ReentrantLock解决死锁问题
 */
public class DeadLockDemo2 {
    private static ReentrantLock lock1 = new ReentrantLock();
    private static ReentrantLock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(()->{
            try {
                boolean flag1 = lock1.tryLock(100, TimeUnit.MILLISECONDS);
                if (flag1){
                    try {
                        System.out.println("线程1获取到lock1");
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        System.out.println("Thread 1 waiting lock2");
                        boolean flag2 = lock2.tryLock();
                        if (flag2){
                            try {
                                System.out.println("线程1获取到lock2");
                            }finally {
                                System.out.println("线程1释放lock2");
                                lock2.unlock();
                            }
                        }else{
                            System.out.println("线程1没抢到锁2");
                        }
                    }finally {
                        System.out.println("线程1释放锁1");
                        lock1.unlock();
                    }
                }else {
                    System.out.println("线程1没抢到锁1");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()->{
            try {
                boolean flag1 = lock2.tryLock(100,TimeUnit.MILLISECONDS);
                if (flag1){
                    try {
                        System.out.println("线程2获取到lock2");
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        System.out.println("Thread 1 waiting lock2");
                        boolean flag2 = lock1.tryLock();
                        if (flag2){
                            try {
                                System.out.println("线程2获取到lock1");
                            }finally {
                                System.out.println("线程2释放lock1");
                                lock1.unlock();
                            }

                        }else{
                            System.out.println("线程2没抢到锁1");
                        }
                    }finally {
                        System.out.println("线程2释放锁2");
                        lock2.unlock();
                    }
                }else {
                    System.out.println("线程2没抢到锁2");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
